package com.tidc.seaserverservice.service.impl;

import com.tidc.seaservermapper.dao.UserDao;
import com.tidc.seaservermodel.dto.UserDto;
import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.Menu;
import com.tidc.seaservermodel.entity.Role;
import com.tidc.seaservermodel.entity.User;
import com.tidc.seaserverservice.service.VueService;
import com.tidc.seaserverservice.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020/9/12 20:25
 * @description：
 * @modified By：
 * @version:
 */
@Service
@Slf4j
public class VueServiceImpl implements VueService {
    @Resource
    private UserUtil userUtil;
    @Resource
    private UserDao userDao;
    @Override
    public Fres<UserDto> getMenu(Authentication authentication) {
        Fres<UserDto> fres = null;
        if(authentication.getName()==null){
            return Fres.unAuthentication(null, "");
        }
        User user = userDao.queryById(Integer.valueOf(authentication.getName()));
        UserDto userInfo = null;
        if(user!=null){
            userInfo = userUtil.getUserInfo(user);
            List<Menu> menus = userInfo.getMenus();
            for (Menu menu : menus) {
                menu.setChild(new ArrayList<>());
                for (Menu menu1 : menus) {
                    if (menu1.getParentid()!=null && menu1.getParentid().equals(menu.getId())) {
                        menu.getChild().add(menu1);
                    }
                }
            }
            for (int i = 0; i < menus.size(); i++) {
                if (menus.get(i).getParentid()!=null) {
                    menus.remove(i);
                    i--;
                }
            }
        }else{
            log.error("id为 {} 的用户不存在",authentication.getName());
        }

        if(userInfo!=null){
            userInfo.setRoles(null);
            userInfo.getUser().setPassword(null);
            fres = Fres.Success(userInfo,"成功");
        }else {
            fres = Fres.unLogin(null,"重新登入");
        }
        return fres;
    }


}
